package org.sz;

import java.util.*;
import java.io.Serializable;

/**
 * MySQL资源配置类
 * 负责存储数据库连接参数、查询配置及连接池配置
 */
public class MySQLResource implements Serializable {
    
    // 基础连接参数
    private final String host;         // 数据库主机地址
    private final int port;            // 数据库端口
    private final String database;     // 数据库名称
    private final String username;     // 连接用户名
    private final String password;     // 连接密码
    
    // 查询配置
    private final String query;                // 执行的SQL查询
    private final List<String> contentColumns; // 文档内容包含的列
    private final List<String> metadataColumns; // 文档元数据包含的列
    
    // 连接池配置
    private int maximumPoolSize = 10;          // 最大连接数
    private int minimumIdle = 5;               // 最小空闲连接数
    private long connectionTimeout = 30000;    // 连接超时时间(毫秒)
    private long idleTimeout = 600000;         // 空闲连接超时时间(毫秒)
    private long maxLifetime = 1800000;        // 连接最大生命周期(毫秒)
    private Properties connectionProperties;   // 额外连接属性
    
    // 数据源标识
    public static final String SOURCE = "mysql";
    
    // 默认连接参数
    public static final String DEFAULT_HOST = "127.0.0.1";
    public static final int DEFAULT_PORT = 3306;
    public static final String DEFAULT_USERNAME = "root";
    public static final String DEFAULT_PASSWORD = "root";
    
    /**
     * 全参数构造函数
     */
    public MySQLResource(String host, int port, String database, String username, 
                         String password, String query, List<String> contentColumns, 
                         List<String> metadataColumns) {
        this.host = host;
        this.port = port;
        this.database = database;
        this.username = username;
        this.password = password;
        this.query = query;
        this.contentColumns = contentColumns;
        this.metadataColumns = metadataColumns;
        this.connectionProperties = new Properties();
    }
    
    /**
     * 带默认主机和端口的构造函数
     */
    public MySQLResource(String database, String username, String password, 
                         String query, List<String> contentColumns, 
                         List<String> metadataColumns) {
        this(DEFAULT_HOST, DEFAULT_PORT, database, username, password, 
             query, contentColumns, metadataColumns);
    }
    
    /**
     * 全默认参数的构造函数
     */
    public MySQLResource(String database, String query, 
                         List<String> contentColumns, 
                         List<String> metadataColumns) {
        this(DEFAULT_HOST, DEFAULT_PORT, database, DEFAULT_USERNAME, DEFAULT_PASSWORD, 
             query, contentColumns, metadataColumns);
    }
    
    // Getter方法
    public String getHost() { return host; }
    public int getPort() { return port; }
    public String getDatabase() { return database; }
    public String getUsername() { return username; }
    public String getPassword() { return password; }
    public String getQuery() { return query; }
    public List<String> getTextColumns() { return contentColumns; }
    public List<String> getMetadataColumns() { return metadataColumns; }
    
    /**
     * 生成JDBC连接URL
     */
    public String getJdbcUrl() {
        return String.format("jdbc:mysql://%s:%d/%s", host, port, database);
    }
    
    // 连接池配置的Getter和Setter
    public int getMaximumPoolSize() { return maximumPoolSize; }
    public void setMaximumPoolSize(int maximumPoolSize) { this.maximumPoolSize = maximumPoolSize; }
    
    public int getMinimumIdle() { return minimumIdle; }
    public void setMinimumIdle(int minimumIdle) { this.minimumIdle = minimumIdle; }
    
    public long getConnectionTimeout() { return connectionTimeout; }
    public void setConnectionTimeout(long connectionTimeout) { this.connectionTimeout = connectionTimeout; }
    
    public long getIdleTimeout() { return idleTimeout; }
    public void setIdleTimeout(long idleTimeout) { this.idleTimeout = idleTimeout; }
    
    public long getMaxLifetime() { return maxLifetime; }
    public void setMaxLifetime(long maxLifetime) { this.maxLifetime = maxLifetime; }
    
    public Properties getConnectionProperties() { return connectionProperties; }
    
    /**
     * 添加额外的连接属性
     */
    public void addConnectionProperty(String key, String value) {
        connectionProperties.setProperty(key, value);
    }
}